import { dealChannelList } from '@/apis/clue'
import { DICT_DATA } from '@/utils/dict-data'

/**
 * 定义搜索每项的接口内容
 */
export interface searchItemInterface {
    key: string
    type: string
    modelValue?: string | number | undefined | boolean | string[] | Date[]
    timeFormat?: string
    placeholder?: string
    clearable?: boolean
    name?: string
    width?: number
    change?: (arg?: any) => void
    required?: boolean
    options?: any[]
}

/**
 * 定义一个基础的数据
 */
const basicValue: searchItemInterface = {
    key: '',
    placeholder: '',
    type: 'text',
    timeFormat: 'YYYY-MM-DD', //时间组件返回的格式 'YYYY-MM-DD'=》 年月日 ，'x'=》 时间戳
    options: [],
    clearable: true,
    modelValue: '',
    name: '',
    width: 160,
    change: (arg?: any) => {},
    required: false
}

/**
 * 默认存在的list
 */
const defaultList: searchItemInterface[] = [
    {
        key: 'slot',
        type: 'slot'
    },
    {
        key: 'status',
        type: 'select',
        name: '状态',
        width: 160,
        modelValue: '',
        change: (arg?: any) => {},
        options: [
            {
                value: '0',
                label: '开启'
            },
            {
                value: '1',
                label: '关闭'
            }
        ]
    }
]

export class SearchData {
    list: Array<searchItemInterface>
    constructor(list: Array<searchItemInterface | string | string[]>) {
        this.list = []

        const arr: Array<searchItemInterface | string | string[]> = [...list]
        //创建map数据结构
        const map = new Map()

        /**
         * 然后遍历数组
         */
        arr.forEach((item: searchItemInterface | string | string[]) => {
            /**
             * 判断item值是object还是string
             */
            if (Object.prototype.toString.call(item) === '[object Object]') {
                /**
                 * 自定义对象，用于接收 item 变量
                 */
                const obj = item as searchItemInterface
                if (!map.has(obj.key)) {
                    const basicObj: searchItemInterface = JSON.parse(JSON.stringify(basicValue))
                    const result = Object.assign(basicObj, obj)
                    // console.log('aaaaaaa', result.key, DICT_DATA.hasMap(result.key))

                    if (result.key === 'transactionChannel') {
                        //获取成交渠道下拉列表
                        dealChannelList().then(res => {
                            if (Number(res.data.code) === 200) {
                                const arr = res.data.result.map((item: any) => {
                                    return {
                                        label: item,
                                        value: item
                                    }
                                })
                                result.options!.length = 0
                                result.options?.push(...arr)
                            }
                        })
                    } else if (DICT_DATA.has(result.key)) {
                        result.options!.length = 0
                        result.options?.push(...DICT_DATA.get(result.key))
                    }
                    this.list.push(result)
                    map.set(obj.key, obj)
                }
            } else if (Object.prototype.toString.call(item) === '[object String]') {
                /**
                 * 默认字符串
                 * 用默认的字符串 与 默认的 list 进行对比
                 * 有则添加进 this.list , 反之不做任何操作
                 */
                const obj = item as string
                if (!map.has(obj)) {
                    defaultList.map(m => {
                        if (m.key === item) {
                            this.list.push(m)
                        }
                    })
                    map.set(obj, obj)
                }
            } else if (Object.prototype.toString.call(item) === '[object Array]') {
                /**
                 * 数组
                 */
                const key: string[] = item as []
                this.list.push({
                    key: 'custom',
                    type: 'slot',
                    width: 160,
                    name: key[0]
                })
            }
        })

        // console.log(this.list)
    }
}
